package com.jic.data.exception;

import com.jic.data.annotation.ResponseMessage;
import com.jic.data.constant.consist.ResponseCode;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * @author zhoufeng
 * @Description:
 * @date 2020-04-09 14:56
 */
@Data
public class AppRuntimeException extends RuntimeException {

    private static final Logger logger = LoggerFactory.getLogger(AppRuntimeException.class);

    private String status;
    private String message;
    private Object data;
    private String moreInfo;

    static {
        Arrays.stream(ResponseCode.class.getDeclaredFields())
            .forEach(p -> {
                try {
                    ResponseMessage annotation = p.getAnnotation(ResponseMessage.class);
                    System.setProperty(String.valueOf(p.get("value")), annotation.value());
                } catch (Exception e) {
                    logger.error("fail to load error code:  " + p.getName());
                }
            });
    }

    public AppRuntimeException(String status, Object... msg) {
        String desc = System.getProperty(String.valueOf(status));
        StringBuilder buffer = new StringBuilder();
        buffer.append(desc);
        if (msg != null && msg.length > 0){
            for (Object aMsg : msg) {
                if (aMsg != null) {
                    buffer.append("，").append(aMsg.toString());
                }
            }
        }
        this.status = status;
        this.message = buffer.toString();
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        // suppress error message
        return this;
    }

    public AppRuntimeException() {}

}
